//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.chris.serve;

import com.alibaba.fastjson.JSONObject;
import com.chris.entity.reader;
import com.chris.impl.loanImpl;
import com.chris.impl.readerImpl;
import com.chris.impl.registerImpl;
import com.chris.utils.DbConnection;
import com.chris.utils.JsonProcess;
import com.chris.utils.LogUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet({"/returnJournal"})
public class returnJournal extends HttpServlet {
    private final loanImpl dao = new loanImpl();
    private final readerImpl dao2 = new readerImpl();
    private final registerImpl dao3 = new registerImpl();

    public returnJournal() {

    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletInputStream inputStream = req.getInputStream();
        JSONObject read = JsonProcess.read(inputStream);
        PrintWriter writer = resp.getWriter();
        DbConnection.begin();
        try {
            String account = (String)read.get("account");
            int journal_id = (Integer)read.get("journal_id");
            long return_time = (Long)read.get("return_time");
            long borrow_time = (Long)read.get("borrow_time");
            int bookNum = this.dao2.getUserByAccount(account).getBookNum();
            this.dao2.setReaderNum(bookNum + 1, account);
            this.dao3.Borrowed(journal_id, 0);
            Date return_time1 = new Date(return_time);
            Date borrow_time1 = new Date(borrow_time);
            this.dao.return_journal(account, journal_id, borrow_time1, return_time1);
            reader userByAccount = dao2.getUserByAccount(account);
            int num = userByAccount.getBookNum();//剩余借书的数量
            writer.print(JsonProcess.respSuccess("还书成功", num));
            DbConnection.commit();
        } catch (Exception var15) {
            LogUtil.log("参数错误",var15);
            writer.print(JsonProcess.respError("参数错误"));
            DbConnection.rollBack();
        }
    }
    public void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.addHeader("Access-Control-Allow-Headers", "*");
    }
}
